//
//  HRVisitorView.swift
//  weiBo
//
//  Created by helong on 16/7/9.
//  Copyright © 2016年 helong. All rights reserved.
//

import UIKit
import SnapKit
class HRVisitorView: UIView {

    //定义注册按钮 和 登录按钮 的闭包,将点击事件传递给控制器 因为点击的时候需要让控制器去push
    var registButtonClosure: (() -> ())?
    var loginButtonClosure: (() -> ())?
    
    //懒加载旋转图片
    private lazy var circle: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

    //懒加载遮罩图片
    private lazy var cover: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

    //懒加载房子
    private lazy var home:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //懒加载label
    private lazy var label: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFontOfSize(14)
        lbl.textAlignment = .Center
        lbl.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lbl.numberOfLines = 0
        return lbl
    }()
    //懒加载注册按钮
    private lazy var registButton: UIButton = {
        //设置注册按钮的各种属性
        let btn = UIButton()
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //给注册按钮添加点击事件
        btn.addTarget(self, action: "registButtonClick", forControlEvents: .TouchUpInside)
        return btn
    }()
    //懒加载登录按钮
    private lazy var logButton: UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //给登录按钮添加点击事件
        btn.addTarget(self, action: "loginButtonClick", forControlEvents: .TouchUpInside)
        return btn
    }()
    //MARK: - 注册和登录按钮的点击事件
    //登录按钮的点击事件
    func loginButtonClick(){
        self.loginButtonClosure!()
    }
    //注册按钮的点击事件
    func registButtonClick(){
        self.registButtonClosure!()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)

        setUI()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 布局子控件
    /**
     设置所有控件的约束
     */
    @objc private func setUI(){
        self.backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        addSubview(circle)
        addSubview(cover)
        addSubview(home)
        addSubview(label)
        addSubview(logButton)
        addSubview(registButton)
        circle.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        cover.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        home.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        label.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(cover.snp_bottom)
            make.width.equalTo(224)
        }
        registButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(label.snp_leading)
            make.top.equalTo(label.snp_bottom).offset(10)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        logButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(label.snp_trailing)
            make.top.equalTo(label.snp_bottom).offset(10)
            make.size.equalTo(CGSizeMake(100, 35))
        }
    }
    //MARK: - 设置旋转动画
    /**
     设置circle的选装动画
     */
    func animationForCircle(){
        //设置动画的方式为旋转
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画的旋转角度
        animate.toValue = M_PI * 2
        animate.repeatCount = MAXFLOAT
        animate.duration = 20
        //当切换屏幕的时候动画不会停止
        animate.removedOnCompletion = false
        circle.layer.addAnimation(animate, forKey: nil)
    }
    //MARK: - 自定义的给外界调用的方法
    /**
     给外界设置各个模块的接口
     
     - parameter title:     label中的文字
     - parameter imageName: 各模块中的图片
     */
    func setUI(title: String?,imageName: String?){
        if let newTitle = title,let newImageName = imageName{//如果外界传入的参数都不为空,则判定为不是首页,那么我们要隐藏旋转视图
            
            home.image = UIImage(named: newImageName)
            label.text = newTitle
            circle.hidden = true
        }else{//否则不不隐藏,打开动画
            circle.hidden = false
            animationForCircle()
    }
    }
}
